/**
 * @author phoenics@126com
 * @date 2017年11月13日 上午9:22:01
 * @version V1.0
 */

package com.jx.gocom.nlp.classify.webservice.service.impl;

import java.util.List;

import org.springframework.stereotype.Service;

import com.github.sd4324530.jtuple.Tuple2;
import com.jx.gocom.nlp.classify.webservice.service.EvaluateBayes;

/**
 *
 */
@Service
public class EvaluateBayesImpl implements EvaluateBayes {
	private static org.slf4j.Logger logger = org.slf4j.LoggerFactory.getLogger(EvaluateBayesImpl.class);
	/**
     * 计算准确率
     * @param reallist 真实类别
     * @param pridlist 预测类别
     * @return
     */
    public double evaluate(List<String> reallist, List<String> pridlist){
        double correctNum = 0.0;
        for (int i = 0; i < reallist.size(); i++) {
            if(reallist.get(i).equals(pridlist.get(i))){
                correctNum += 1.0;
            }
        }
        double accuracy = correctNum / (double)reallist.size();
        System.out.println("准确率为：" + accuracy);
        return accuracy;
    }
    /**
     * 计算精确率和召回率
     * @param reallist
     * @param pridlist
     * @param classname
     */
    public Tuple2<Double, Double> calPreRec(List<String> reallist, List<String> pridlist, String classname){
        double correctNum = 0.00000001;
        double allNum = 0.00000001;//测试数据中，某个分类的文章总数
        double preNum = 0.00000001;//测试数据中，预测为该分类的文章总数
        
        for (int i = 0; i < reallist.size(); i++) {
        	 //logger.info("r=={}===classname=={}",reallist.get(i),classname);
            if(reallist.get(i).equals(classname)){
                allNum += 1.0;
                if(reallist.get(i).equals(pridlist.get(i))){
                    correctNum += 1.0;
                }
            }
            if(pridlist.get(i).equals( classname)){
                preNum += 1.0;
            }
        }
        System.out.println(classname + " 精确率(跟预测分类比较):" + correctNum / preNum + " 召回率（跟真实分类比较）:" + correctNum / allNum);
        return Tuple2.with(correctNum / preNum,  correctNum / allNum);
    }
}
